<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="logo" />
        <span>海报设计系统</span>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" prefix-icon="el-icon-unlock" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="doSubmit()">登录</el-button>
        </el-form-item>
        <el-row style="text-align: center;margin-top:-10px">
          <el-link type="primary" @click="register">没有账号？点击注册</el-link>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import users from '../assets/data/users.json';

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
      },
    }
  },
  methods: {
    register() {
      this.$router.push('/register');
    },
    doSubmit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const user = users.find(user => user.username === this.loginForm.username && user.password === this.loginForm.password);
          if (user) {
            console.log("登录成功");
            // 执行登录逻辑
          } else {
            console.log("账号或密码错误");
            this.$message.error("账号或密码错误");
          }
        } else {
          console.log("表单验证失败");
        }
      });
    }
  }
}
</script>

<style scoped>
/* 你的样式代码 */
</style>
